package l1j.server.server.clientpackets;

import java.util.Timer;
import java.util.TimerTask;

import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.Server.L1World.WorldClan;
import l1j.server.echo.ClientThread;
import l1j.server.server.ActionCodes;
import l1j.server.server.datatables.HouseTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1DoorInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1House;

import java.util.logging.Level;
import java.util.logging.Logger;
// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket, C_Door

/**
 * 處理收到由客戶端傳來開關門的封包
 */
public class C_Door extends ClientBasePacket {

	public class CloseTimer extends TimerTask {

		private final L1DoorInstance _door;

		public CloseTimer(L1DoorInstance door) {
			this._door = door;
		}

		public void begin() {
			final Timer timer = new Timer();
			timer.schedule(this, 5 * 1000);
		}

		@Override
		public void run() {
			if (this._door.getOpenStatus() == ActionCodes.ACTION_Open) {
				this._door.close();
			}
		}
	}
	private static final String C_DOOR = "[C] C_Door";
	private static final Logger _log = Logger.getLogger(C_Door.class.getName());

	@Override
	public String getType() {
		return C_DOOR;
	}

	private boolean isExistKeeper(L1PcInstance pc, int keeperId) {
		if (keeperId == 0) {
			return false;
		}

		final L1Clan clan = WorldClan.get().getClan(pc.getClanname());
		if (clan != null) {
			final int houseId = clan.getHouseId();
			if (houseId != 0) {
				final L1House house = HouseTable.getInstance().getHouseTable(houseId);
				if (keeperId == house.getKeeperId()) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}

			this.readH();
			this.readH();
			final int objectId = this.readD();
			final L1Object obj = L1World.getInstance().findObject(objectId);
			if(!(obj instanceof L1DoorInstance)){
				return;
			}
			final L1DoorInstance door = (L1DoorInstance) L1World.getInstance().findObject(objectId);
			if (door == null) {
				return;
			}

			if (door.getDoorId() >= 5001 && door.getDoorId() <= 5010) { // 水晶洞窟
				return;
			}
			else if (door.getDoorId() == 6006) { // 冒險洞穴二樓
				if (door.getOpenStatus() == ActionCodes.ACTION_Open) {
					return;
				}
				if (pc.getInventory().consumeItem(40163, 1)) { // 黃金鑰匙
					door.open();
					final CloseTimer closetimer = new CloseTimer(door);
					closetimer.begin();
				}
			}
			else if (door.getDoorId() == 6007) { // 冒險洞穴二樓
				if (door.getOpenStatus() == ActionCodes.ACTION_Open) {
					return;
				}
				if (pc.getInventory().consumeItem(40313, 1)) { // 銀鑰匙
					door.open();
					final CloseTimer closetimer = new CloseTimer(door);
					closetimer.begin();
				}
			}
			else if (!this.isExistKeeper(pc, door.getKeeperId())) {
				if (door.getOpenStatus() == ActionCodes.ACTION_Open) {
					door.close();
				}
				else if (door.getOpenStatus() == ActionCodes.ACTION_Close) {
					door.open();
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
